var Crawler = require("crawler");
var fs = require('fs');

var arr=[];//保存职位链接
var date=[];//保存职位数据
var start=0;//记录爬取数量
var end;//职位总数量
var link='http://search.51job.com/jobsearch/search_result.php?fromJs=1&jobarea=030200%2C00&district=000000&funtype=0000&industrytype=00&issuedate=9&providesalary=07%2C08%2C09&keyword=%E5%89%8D%E7%AB%AF&keywordtype=2&curr_page=1&lang=c&stype=1&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&list_type=0&dibiaoid=0&confirmdate=9'
//爬取职位链接
var crawler = new Crawler({
	    maxConnections: 10,
	    // This will be called for each crawled page
	    forceUTF8:true,
	    callback: function(error, res, done) {
	        if (error) {
	            console.log(error);
	        } else {
	            var $ = res.$;

	            var link=[];
	            $('#resultList .el:not(.title)').each(function (idx, element) {
				        var $element = $(element);
				        var plink=$element.find('.t1 a').attr('href');
				        if(plink!=undefined){
				        	arr.push(plink);
				        	desc.queue(plink);
				        }
				        
				 });
	            end=$('#resultList .el:not(.title)').length;
	            
	        };

	    }
	});
crawler.queue(link);

//爬取职位详细
var desc = new Crawler({
	    maxConnections: 1000,
	    // This will be called for each crawled page
	    forceUTF8:true,
	    callback: function(error, res, done) {
	        if (error) {
	            console.log(error);
	        } else {
	            var $ = res.$;

	            var xlink=res.options.uri

	            //职位名字
	            var ctitle=$('.tHeader h1').attr('title');
	            if(ctitle!=undefined){
	            	xtitle=unescape(ctitle.replace(/(^\s+)|(\s+$)/g,"").replace(/&#x/g, '%u').replace(/;/g, ''));
	            }
	            

	            //公司名字
	            var cname=$('.tHeader .cname a').attr('title');
	            if(cname!=undefined){
	            	xname=unescape(cname.replace(/(^\s+)|(\s+$)/g,"").replace(/&#x/g, '%u').replace(/;/g, ''));
	            }
	            var xnameLink=$('.tHeader .cname a').attr('href');

	            //所在区域
	            var lname=$('.tHeader .lname').text();
	           
	            //薪水范围
	            var cmoney=$('.tHeader strong').html();
	            if(cmoney!=undefined){
	            	xmoney=unescape(cmoney.replace(/(^\s+)|(\s+$)/g,"").replace(/&#x/g, '%u').replace(/;/g, ''));
	            }
	            

	            //公司地址
	            var cadd=$('.bmsg.inbox:not(.job_msg) .fp').html();
	            if(cadd!=undefined){
	            	xadd=unescape(cadd.replace(/(^\s+)|(\s+$)/g,"").replace(/&#x/g, '%u').replace(/;/g, ''));
	            }
	            

	            //工作要求
	            $('.bmsg.job_msg.inbox a,.bmsg.job_msg.inbox .mt10,.bmsg.job_msg.inbox .label').remove();
	            var ctxt=$('.bmsg.job_msg.inbox').html();
	            if(ctxt!=undefined){
	            	xtxt=unescape(ctxt.replace(/(^\s+)|(\s+$)/g,"").replace(/&#x/g, '%u').replace(/;/g, ''));
	            }
	            
	            //保存到数据
	            var news = {
	            	"id":start,
					"title": xtitle,
					"name": xname,
					"namelink": xnameLink,
					"money": xmoney,
					"txtle": xtxt,
					"address":xadd,
					"laddress":lname,
					"link":xlink,
					"start":false
				};
				date.push(news);

				start++;
				if(start==end){
					fs.writeFileSync('./js/job.js', JSON.stringify(date));
					console.log('保存成功！')
				}

	        };

	    }
	});